import _ from 'lodash-es'

/**
 * 参数处理
 * @param {*} params  参数
 */
export function transParams(params: any) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    const part = encodeURIComponent(propName) + '='
    // if (value !== null && value !== '' && typeof (value) !== 'undefined') {
    if (value !== null && typeof value !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          // if (value[key] !== null && value[key] !== '' && typeof (value[key]) !== 'undefined') {
          if (value[key] !== null && typeof value[key] !== 'undefined') {
            const params = propName + '[' + key + ']'
            const subPart = encodeURIComponent(params) + '='
            result += subPart + encodeURIComponent(value[key]) + '&'
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&'
      }
    }
  }
  return result ? result.slice(0, -1) : result
}

export const prependIfMissing = (str: string | undefined, prefix: string | undefined) => {
  if (str === undefined || prefix === undefined) {
    return str
  }
  return _.startsWith(str, prefix) ? str : `${prefix}${str}`
}

export const removeSuffix = (str: string | undefined, suffix: string | undefined) => {
  if (str === undefined || suffix === undefined) {
    return str
  }
  return _.endsWith(str, suffix) ? str.slice(0, -suffix.length) : str
}

export const startsWithAny = (str: string | undefined, ...prefixes: string[]) => {
  if (str === undefined || !prefixes || !prefixes.length) {
    return false
  }
  return _.some(prefixes, (prefix) => _.startsWith(str, prefix))
}

export const isBlankString = (str: string | undefined | null): boolean => {
  return str === undefined || str === null || str.trim().length === 0
}

export const subString = (str: string, start: number, end: number) => {
  if (str == null || str === '') return ''
  if (start < 0) start = 0
  if (end > str.length) end = str.length
  if (start >= end) return ''
  return str.slice(start, end)
}

/**
 * html是否包含可见文本内容
 * @param html
 */
export const isHtmlHasText = (html: string | undefined | null) => {
  if (isBlankString(html)) {
    return false
  }
  const div = document.createElement('div')
  div.innerHTML = html!
  return !isBlankString(div.innerText)
}

/**
 * html是否包含内容，如：可见文本、图片、视频
 */
export const isHtmlHasContent = (html: string | undefined | null) => {
  return isHtmlHasText(html) || html?.includes('<img ') || html?.includes('<video ')
}

export const isValidJson = (json: string | undefined | null) => {
  const result = _.attempt(JSON.parse, json)
  return !_.isError(result)
}

const fileUrls = import.meta.env.VITE_FILE_BASE_URL?.split(',')
export const s_file_url = (url: any): any => {
  if (!url || !fileUrls || !fileUrls.length) {
    return url
  }
  if (startsWithAny(url, 'https://', 'http://', 'data:', 'blob:', './', '../', 'file://')) {
    return url
  }
  return removeSuffix(_.sample(fileUrls)?.trim(), '/')! + prependIfMissing(url, '/')!
}
